package service.impl;

import bean.User;
import service.UserService;
import utils.BusinessException;
import utils.EmptyUtils;
import utils.UserIO;

public class UserServiceImpl implements UserService {
    @Override
    public User register(User user) throws BusinessException {
        UserIO userIO = new UserIO();
        userIO.add(user);
        userIO.writeUsers();
        return user;
    }

    @Override
    public User login(String username, String password) throws BusinessException {
        //判断用户名和密码是否为空
        if (EmptyUtils.isEmpty(username)) {
            throw new BusinessException("username.notnull");
        }
        if (EmptyUtils.isEmpty(password)) {
            throw new BusinessException("password.notnull");
        }
        //查询登入的用户是否注册了
        UserIO userIO = new UserIO();
        User user = userIO.findByUsernameAndPassword(username, password);
        return user;
    }
}
